package com.gurun.camera.account.presenter;

import com.danale.sdk.platform.result.user.CheckForgetPasswordResult;
import com.danale.sdk.platform.result.user.CheckRegistV2Result;
import com.danale.sdk.throwable.PlatformApiError;
import com.gurun.camera.account.model.ISetPasswordModel;
import com.gurun.camera.account.model.SetPasswordModelImpl;
import com.gurun.camera.account.view.ISetPasswordView;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by danal on 2016/9/9.
 */

public class SetPasswordPresenterImpl implements ISetPasswordPresenter {
    private ISetPasswordView setPasswordView;
    private ISetPasswordModel setPasswordModel;
    public SetPasswordPresenterImpl(ISetPasswordView view){
        this.setPasswordView = view;
        setPasswordModel = new SetPasswordModelImpl();
    }

    /**
     * 创建新账户和忘记密码重设账户密码
     * Create a new account and forget the password to reset the account password
     * @param userName 用户名
     *                 account phone number or email
     * @param passWord 密码
     * @param verificationCode 验证码
     * @param phoneCode 国别码
     *                  If userName is the phone number, phonecode is the country code of the country where the phone number belongs.
                        If userName is a email, phonecode is empty;
     * @param verifytype 1，创建新账户  2，忘记密码
     *                   1.create new account 2.forgot password
     */
    @Override
    public void createAccount(String userName, String passWord, String verificationCode, String phoneCode, int verifytype) {

        if(verifytype == 2){
            setPasswordModel.forgotPwdResetAccount(userName,passWord,verificationCode,phoneCode)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<CheckForgetPasswordResult>() {
                        @Override
                        public void call(CheckForgetPasswordResult checkForgetPasswordResult) {
                            if(setPasswordView != null){
                                setPasswordView.notifyCreateAccountResult("SUCCESS");

                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            if(setPasswordView != null){
                                setPasswordView.notifyCreateAccountResult(((PlatformApiError) throwable).getErrorDescription());

                            }
                        }
                    });

        }else{

            setPasswordModel.createAccount(userName,passWord,verificationCode,phoneCode)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<CheckRegistV2Result>() {
                        @Override
                        public void call(CheckRegistV2Result checkRegistV2Result) {
                            if(setPasswordView != null){
                                setPasswordView.notifyCreateAccountResult("SUCCESS");

                            }


                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            if(throwable instanceof PlatformApiError){
                                if(setPasswordView != null){
                                    setPasswordView.notifyCreateAccountResult(((PlatformApiError) throwable).getErrorDescription());
                                }
                            }


                        }
                    });

        }

    }


}
